﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class DistributionRangeDA : IDistributionRangeDA
    {
        private string ALTERNATE_CARRIER_ID = "@ALTERNATE_CARRIER_ID";
        private string CARRIER_ID = "@CARRIER_ID";
        private string CITY = "@CITY";
        private string CONTINUOUS_WEIGHT = "@CONTINUOUS_WEIGHT";
        private string CONTINUOUS_WEIGHT_PRICE = "@CONTINUOUS_WEIGHT_PRICE";
        private string COUNTIES = "@COUNTIES";
        private string CREATED_BY = "@CREATED_BY";
        private string DISTRICT = "@DISTRICT";
        private string INITIAL_WEIGHT = "@INITIAL_WEIGHT";
        private string INITIAL_WEIGHT_PRICE = "@INITIAL_WEIGHT_PRICE";
        private string PROVINCE = "@PROVINCE";
        private string ROWID = "@ROWID";
        private string SQL_CHECK_DISTRIBUTION_RANGE_ID_UNIQUE = " SELECT COUNT(1) FROM @_@DISTRIBUTION_RANGE WHERE ROWID = @ROWID ";
        private string SQL_DELETE_DISTRIBUTION_RANGE = " DELETE FROM @_@DISTRIBUTION_RANGE WHERE ROWID = @ROWID ";
        private string SQL_INSERT_DISTRIBUTION_RANGE = " INSERT INTO @_@DISTRIBUTION_RANGE (WH_ID,PROVINCE, CITY, COUNTIES, DISTRICT, CARRIER_ID, INITIAL_WEIGHT, INITIAL_WEIGHT_PRICE, CONTINUOUS_WEIGHT, CONTINUOUS_WEIGHT_PRICE, ALTERNATE_CARRIER_ID, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE) VALUES ( @WH_ID,@PROVINCE, @CITY, @COUNTIES, @DISTRICT, @CARRIER_ID, @INITIAL_WEIGHT, @INITIAL_WEIGHT_PRICE, @CONTINUOUS_WEIGHT, @CONTINUOUS_WEIGHT_PRICE, @ALTERNATE_CARRIER_ID, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE() )  ";
        private string SQL_SELECT_ALL_DISTRIBUTION_RANGE = " SELECT  ROWID,WH_ID, PROVINCE, CITY, COUNTIES, DISTRICT, CARRIER_ID, INITIAL_WEIGHT, INITIAL_WEIGHT_PRICE, CONTINUOUS_WEIGHT, CONTINUOUS_WEIGHT_PRICE, ALTERNATE_CARRIER_ID, TS, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE  FROM  @_@DISTRIBUTION_RANGE ";
        private string SQL_UPDATE_DISTRIBUTION_RANGE = " UPDATE @_@DISTRIBUTION_RANGE SET PROVINCE = @PROVINCE,WH_ID=@WH_ID, CITY = @CITY, COUNTIES = @COUNTIES, DISTRICT = @DISTRICT, CARRIER_ID = @CARRIER_ID, INITIAL_WEIGHT = @INITIAL_WEIGHT, INITIAL_WEIGHT_PRICE = @INITIAL_WEIGHT_PRICE, CONTINUOUS_WEIGHT = @CONTINUOUS_WEIGHT, CONTINUOUS_WEIGHT_PRICE = @CONTINUOUS_WEIGHT_PRICE, ALTERNATE_CARRIER_ID = @ALTERNATE_CARRIER_ID, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE ROWID = @ROWID  ";
        private string TS = "@TS";
        private string UPDATED_BY = "@UPDATED_BY";
        private string WH_ID = "@WH_ID";

        public bool CheckDistributionRangeIDUnique(string distributionRangeID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ROWID", distributionRangeID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_DISTRIBUTION_RANGE_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckDistributionRangeIDUnique(DataBase dataBase, DbTransaction tran, string distributionRangeID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ROWID", distributionRangeID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_DISTRIBUTION_RANGE_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeleteDistributionRange(List<string> distributionRangeID, string whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int j = 0; j < distributionRangeID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ROWID", distributionRangeID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_DISTRIBUTION_RANGE.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteDistributionRange(DataBase dataBase, DbTransaction tran, List<string> distributionRangeID, string whLoginID)
        {
            int result = 0;
            try
            {
                for (int j = 0; j < distributionRangeID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ROWID", distributionRangeID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_DISTRIBUTION_RANGE.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllDistributionRange(string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_DISTRIBUTION_RANGE.Replace("@_@", whLoginID);
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllDistributionRange(DataBase dataBase, DbTransaction tran, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_DISTRIBUTION_RANGE.Replace("@_@", whLoginID);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public DistributionRangeInfo GetDistributionRangeByID(string distributionrangeID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_DISTRIBUTION_RANGE.Replace("@_@", whLoginID) + " WHERE ROWID = @ROWID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ROWID", distributionrangeID) };
            DistributionRangeInfo distributionRangeInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    distributionRangeInfo = new DistributionRangeInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["PROVINCE"].ToString(), reader["CITY"].ToString(), reader["COUNTIES"].ToString(), reader["DISTRICT"].ToString(), reader["CARRIER_ID"].ToString(), (reader["INITIAL_WEIGHT"].ToString() != "") ? decimal.Parse(reader["INITIAL_WEIGHT"].ToString()) : 0M, (reader["INITIAL_WEIGHT_PRICE"].ToString() != "") ? decimal.Parse(reader["INITIAL_WEIGHT_PRICE"].ToString()) : 0M, (reader["CONTINUOUS_WEIGHT"].ToString() != "") ? decimal.Parse(reader["CONTINUOUS_WEIGHT"].ToString()) : 0M, (reader["CONTINUOUS_WEIGHT_PRICE"].ToString() != "") ? decimal.Parse(reader["CONTINUOUS_WEIGHT_PRICE"].ToString()) : 0M, reader["ALTERNATE_CARRIER_ID"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
                }
            }
            return distributionRangeInfo;
        }

        public DistributionRangeInfo GetDistributionRangeByID(DataBase dataBase, DbTransaction tran, string distributionrangeID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_DISTRIBUTION_RANGE.Replace("@_@", whLoginID) + " WHERE ROWID = @ROWID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ROWID", distributionrangeID) };
            DistributionRangeInfo distributionRangeInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                distributionRangeInfo = new DistributionRangeInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["PROVINCE"].ToString(), reader["CITY"].ToString(), reader["COUNTIES"].ToString(), reader["DISTRICT"].ToString(), reader["CARRIER_ID"].ToString(), (reader["INITIAL_WEIGHT"].ToString() != "") ? decimal.Parse(reader["INITIAL_WEIGHT"].ToString()) : 0M, (reader["INITIAL_WEIGHT_PRICE"].ToString() != "") ? decimal.Parse(reader["INITIAL_WEIGHT_PRICE"].ToString()) : 0M, (reader["CONTINUOUS_WEIGHT"].ToString() != "") ? decimal.Parse(reader["CONTINUOUS_WEIGHT"].ToString()) : 0M, (reader["CONTINUOUS_WEIGHT_PRICE"].ToString() != "") ? decimal.Parse(reader["CONTINUOUS_WEIGHT_PRICE"].ToString()) : 0M, reader["ALTERNATE_CARRIER_ID"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return distributionRangeInfo;
        }

        public DataSet GetDistributionRangeByQueryList(List<string> sqlWhere, DistributionRangeQueryEntity distributionrangeQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_DISTRIBUTION_RANGE.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!distributionrangeQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, distributionrangeQuery.CurrentPage, distributionrangeQuery.PageSize, distributionrangeQuery.SortField, distributionrangeQuery.SortDirection);
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetDistributionRangeByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, DistributionRangeQueryEntity distributionrangeQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_DISTRIBUTION_RANGE.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            temp = PagingHelper.GetPagingSQL(temp, distributionrangeQuery.CurrentPage, distributionrangeQuery.PageSize, distributionrangeQuery.SortField, distributionrangeQuery.SortDirection);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public DistributionRangeInfo GetDistributionRangeInfoByShipping(DataBase dataBase, DbTransaction tran, string city, string counties, string prov, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_DISTRIBUTION_RANGE.Replace("@_@", whLoginID) + " WHERE PROVINCE=@PROVINCE AND CITY=@CITY AND COUNTIES=@COUNTIES";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@PROVINCE", prov), new SqlParameter("@CITY", city), new SqlParameter("@COUNTIES", counties) };
            DistributionRangeInfo distributionRangeInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                distributionRangeInfo = new DistributionRangeInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["PROVINCE"].ToString(), reader["CITY"].ToString(), reader["COUNTIES"].ToString(), reader["DISTRICT"].ToString(), reader["CARRIER_ID"].ToString(), (reader["INITIAL_WEIGHT"].ToString() != "") ? decimal.Parse(reader["INITIAL_WEIGHT"].ToString()) : 0M, (reader["INITIAL_WEIGHT_PRICE"].ToString() != "") ? decimal.Parse(reader["INITIAL_WEIGHT_PRICE"].ToString()) : 0M, (reader["CONTINUOUS_WEIGHT"].ToString() != "") ? decimal.Parse(reader["CONTINUOUS_WEIGHT"].ToString()) : 0M, (reader["CONTINUOUS_WEIGHT_PRICE"].ToString() != "") ? decimal.Parse(reader["CONTINUOUS_WEIGHT_PRICE"].ToString()) : 0M, reader["ALTERNATE_CARRIER_ID"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return distributionRangeInfo;
        }

        public int InsertDistributionRange(DistributionRangeInfo distributionRangeInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                distributionRangeInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_DistributionRange_Parameters(distributionRangeInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_DISTRIBUTION_RANGE.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int InsertDistributionRange(DataBase dataBase, DbTransaction tran, DistributionRangeInfo distributionRangeInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                distributionRangeInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_DistributionRange_Parameters(distributionRangeInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_DISTRIBUTION_RANGE.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_DistributionRange_Parameters(DistributionRangeInfo distributionRangeInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter(this.PROVINCE, SqlDbType.VarChar, 30), new SqlParameter(this.CITY, SqlDbType.VarChar, 30), new SqlParameter(this.COUNTIES, SqlDbType.VarChar, 50), new SqlParameter(this.DISTRICT, SqlDbType.VarChar, 50), new SqlParameter(this.CARRIER_ID, SqlDbType.VarChar, 20), new SqlParameter(this.INITIAL_WEIGHT, SqlDbType.Decimal, 13), new SqlParameter(this.INITIAL_WEIGHT_PRICE, SqlDbType.Decimal, 13), new SqlParameter(this.CONTINUOUS_WEIGHT, SqlDbType.Decimal, 13), new SqlParameter(this.CONTINUOUS_WEIGHT_PRICE, SqlDbType.Decimal, 13), new SqlParameter(this.ALTERNATE_CARRIER_ID, SqlDbType.VarChar, 20), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 50), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 50), new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.ROWID, SqlDbType.Int) };
            if (!string.IsNullOrEmpty(distributionRangeInfo.Province))
            {
                paramArray[0].Value = distributionRangeInfo.Province;
            }
            else
            {
                paramArray[0].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(distributionRangeInfo.City))
            {
                paramArray[1].Value = distributionRangeInfo.City;
            }
            else
            {
                paramArray[1].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(distributionRangeInfo.Counties))
            {
                paramArray[2].Value = distributionRangeInfo.Counties;
            }
            else
            {
                paramArray[2].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(distributionRangeInfo.District))
            {
                paramArray[3].Value = distributionRangeInfo.District;
            }
            else
            {
                paramArray[3].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(distributionRangeInfo.CarrierID))
            {
                paramArray[4].Value = distributionRangeInfo.CarrierID;
            }
            else
            {
                paramArray[4].Value = DBNull.Value;
            }
            paramArray[5].Value = distributionRangeInfo.InitialWeight;
            paramArray[6].Value = distributionRangeInfo.InitialWeightPrice;
            paramArray[7].Value = distributionRangeInfo.ContinuousWeight;
            paramArray[8].Value = distributionRangeInfo.ContinuousWeightPrice;
            if (!string.IsNullOrEmpty(distributionRangeInfo.AlternateCarrierID))
            {
                paramArray[9].Value = distributionRangeInfo.AlternateCarrierID;
            }
            else
            {
                paramArray[9].Value = DBNull.Value;
            }
            paramArray[10].Value = distributionRangeInfo.CreatedBy;
            paramArray[11].Value = distributionRangeInfo.UpdatedBy;
            paramArray[12].Value = distributionRangeInfo.WhID;
            paramArray[13].Value = distributionRangeInfo.RowID;
            return paramArray;
        }

        public int UpdateDistributionRange(DistributionRangeInfo distributionRangeInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                distributionRangeInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_DistributionRange_Parameters(distributionRangeInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_DISTRIBUTION_RANGE.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int UpdateDistributionRange(DataBase dataBase, DbTransaction tran, DistributionRangeInfo distributionRangeInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                distributionRangeInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_DistributionRange_Parameters(distributionRangeInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_DISTRIBUTION_RANGE.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

